package com.richman.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


import com.richman.controller.BetClient;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;

import com.richman.ps38.entity.Event;


public class RebetWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 16385L;
	private JPanel contentPane;
	private JTextField ps38BetAmtTextField;
	
	private JLabel betTypelb;
	private JLabel betLeglb;
	private JLabel betMatchlb;
	private JLabel ps38bettypelb;
	private JLabel Minbetlb;
	private JLabel Maxbetlb;
	private JLabel hgBetAmtlb;
	
	private JLabel pankoulb;
	private JLabel ratiolabel;

	private JLabel messagelb;


	String typedetail="";
	
	
	String[] item;
	
	String bettype = "";

	/**
	 * Create the frame.
	 */
	public RebetWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 598, 599);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		betTypelb = new JLabel("New label");
		betTypelb.setBounds(23, 28, 213, 15);
		contentPane.add(betTypelb);
		
		betLeglb = new JLabel("New label");
		betLeglb.setBounds(23, 66, 213, 15);
		contentPane.add(betLeglb);
		
		betMatchlb = new JLabel("New label");
		betMatchlb.setBounds(23, 103, 213, 15);
		contentPane.add(betMatchlb);
		
		ps38bettypelb = new JLabel("New label");
		ps38bettypelb.setBounds(23, 156, 213, 15);
		contentPane.add(ps38bettypelb);
		
		Minbetlb = new JLabel("New label");
		Minbetlb.setBounds(23, 283, 213, 15);
		contentPane.add(Minbetlb);
		
		Maxbetlb = new JLabel("New label");
		Maxbetlb.setBounds(342, 283, 213, 15);
		contentPane.add(Maxbetlb);
		
		
		
		
		pankoulb = new JLabel("New label");
		pankoulb.setBounds(23, 199, 213, 15);
		contentPane.add(pankoulb);
		
		
		
		
		
		
		
		
		
		
		JButton refreshBtn = new JButton("刷新");
		refreshBtn.setBounds(23, 385, 109, 34);
		contentPane.add(refreshBtn);
		
		
		
		
		
		
		
		
		
		
		hgBetAmtlb = new JLabel("投注金额：");
		hgBetAmtlb.setBounds(23, 335, 70, 15);
		contentPane.add(hgBetAmtlb);
		
		ps38BetAmtTextField = new JTextField();
		ps38BetAmtTextField.setBounds(90, 332, 66, 21);
		contentPane.add(ps38BetAmtTextField);
		ps38BetAmtTextField.setColumns(10);
		
		JButton betBtn = new JButton("补单");
		betBtn.setBounds(297, 385, 109, 34);
		contentPane.add(betBtn);
		
		messagelb = new JLabel("New label");
		messagelb.setBounds(23, 495, 399, 21);
		contentPane.add(messagelb);
		
		ratiolabel = new JLabel("New label");
		ratiolabel.setBounds(23, 242, 213, 15);
		contentPane.add(ratiolabel);
		
		
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				BetClient.isInManualRebet = false;
			}
		});
		
		
		
		betBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				try {
					
//					if() {
//						dobet();
//					}
					
					
					
				}catch(Exception ex) {
					ex.printStackTrace();
				}
				
				messagelb.setText("投注失败");
				messagelb.setForeground(new Color(255,0,0));
				
			}
		});
		

	}
	
	
//	public void dobet() {
//		DaShuiClient.betCtrMng.
//	}
	
	
	public void setgame(String[] itemarg) {
		
		try{
			
			item = itemarg;
			
			
			
			bettype = item[5];
			bettype = bettype.replace("滚球", "");
			bettype = bettype.replace("单式", "");
			
			
			
			String hometeam = item[4].split("-vs-")[0];
			String awayteam = item[4].split("-vs-")[1];
			
			typedetail = "";
			
			if(item[8].contains(hometeam)) {
				typedetail = "客";
			}else if(item[8].contains(awayteam)) {
				typedetail = "主";
			}else if(item[8].contains("大")) {
				typedetail = "小 ";
			}else if(item[8].contains("小")) {
				typedetail = "大";
			}
			
			
			betTypelb.setText(bettype);
			betLeglb.setText("联赛:" + item[2]);
			betMatchlb.setText("球队:" + item[3]);
			ps38bettypelb.setText("投注类型:" + item[11].split("@")[0]);

			
			ps38BetAmtTextField.setText(item[12]);
			
			
			pankoulb.setText(item[6]);

			
			refreshOdds();
			
			
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		

		
	}
	
	
	
	public void refreshOdds() {
			
			try {
				
				Event event = BetClient.betCtrMng.grabOneEvent(item[15], item[6], typedetail);
				
				if(event == null) {
					messagelb.setText("无投注盘口");
					
					messagelb.setForeground(new Color(255,0,0));
					
					return;
					
				}
				
				
				String betid = "";
				
				//DaShuiClient.betCtrMng.updatePS38Ratio(ps38betid, Long.toString(betgame.event.eventId), bettype, ps38typedetail, ps38betpankou);
				
				if(bettype.contains("大小")) {
					betid = event.wholeDXPBetID;
				}else if(bettype.contains("让球")) {
					betid = event.wholeRQPBetID;
				}
				
				
				Map<String, String> ps38oddUpdres = BetClient.betCtrMng.manualRebetUpdatePS38Ratio(betid, item[15], bettype, typedetail, item[6]);
				
				if(ps38oddUpdres == null) {
					
					messagelb.setText("获取投注信息失败");
					
					messagelb.setForeground(new Color(255,0,0));
					
					return;
				}
				
				
				
				
				ratiolabel.setText("赔率:" + ps38oddUpdres.get("ratio"));
				
				
				Minbetlb.setText("最小投注:"+ps38oddUpdres.get("minbet"));
				Maxbetlb.setText("最大投注:"+ps38oddUpdres.get("maxbet"));
				
				
				int ps38minbetgold = (int)Double.parseDouble(ps38oddUpdres.get("minbet"));
				int ps38maxbetgold = (int)Double.parseDouble(ps38oddUpdres.get("maxbet"));
				
				double ps38betgold = Double.parseDouble(ps38BetAmtTextField.getText());
				
				if(ps38betgold < ps38minbetgold || ps38betgold > ps38maxbetgold) {
					messagelb.setText("PS38投注金额不在投注范围");
					
					messagelb.setForeground(new Color(255,0,0));
					
				}
				
			}catch(Exception e) {
				e.printStackTrace();
			}
		
		
	}
	
}
